<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/28
 * Time: 14:08
 * 点赞
 */

namespace app\api\controller\Index;


use app\api\controller\AppController;
use app\api\model\VideoUser\VideoUserLikeModel;
use app\Request;
use app\Utils\Util;

class UserLike extends AppController
{
    /**
     * 点赞 /取消
     * @param Request $request
     * @param \app\Service\UserLike $like
     * @return array
     */
    public function save(Request $request, \app\Service\UserLike $like)
    {
        $video_id = (int)$request->param('video_id'); //视频ID
        if (empty($video_id)) return Util::returnJson(self::CODE_ERROR, null, "视频ID必传");
        $status = (int)$request->param('status'); //0：取消 1：点赞
        if (! $user = $this->user) return Util::returnJson(self::CODE_LOGIN, null, "token已过期");
        $result = $like->save($user, $video_id, $status);
        if ($result) {
            $result = [];
            $result['num'] = VideoUserLikeModel::where(['video_id' => $video_id, 'status' => 1])->count();
        }
        return Util::returnJson(self::CODE_SUCCESS, $result, "ok");
    }
}